<template>
  <div class="labor-brokerage">
    <div class="title">
      <div class="title-text">劳务撮合板块</div>
    </div>
    <div class="data-display">
      <div class="data-item">
        <p class="item-title">
          <span class="border"></span
          ><count-to
            class="item-number"
            :startVal="0"
            :endVal="personnelCount"
            :duration="1000"
          ></count-to>
        </p>
        <p class="key">产业工人(人)</p>
      </div>
      <div class="data-item">
        <p class="item-title">
          <span class="border"></span
          ><count-to
            class="item-number"
            :startVal="0"
            :endVal="resumeCount"
            :duration="1000"
          ></count-to>
        </p>
        <p class="key">产业工人数字履历(条)</p>
      </div>

      <div class="data-item">
        <p class="item-title">
          <span class="border"></span
          ><count-to
            class="item-number"
            :startVal="0"
            :endVal="certCount"
            :duration="1000"
          ></count-to>
        </p>
        <p class="key">产业工人数字证照(张)</p>
      </div>
    </div>
    <div class="chart-box">
      <div class="chart-title">
        <div class="text">获证人数TOP5</div>
        <div class="legend">
          <span class="legend-color"></span>
          获证人数
        </div>
      </div>
      <Echart id="labor-chart" :options="option" height="229px" width="100%" />
    </div>
  </div>
</template>
  
<script setup>
import { reactive, ref, onMounted, onUnmounted } from "vue";
import Echart from "@/components/echart/index.vue";
import CountTo from "@/components/vueCountTo/vue-countTo.vue";
import { ajax } from "@/store/service";
import opt from "./option";

const option = reactive({ ...opt });
const personnelCount = ref(0);
const resumeCount = ref(0);
const certCount = ref(0);

const getArrByKey = (data, k) => {
  const key = k || "value";
  const res = [];
  if (data) {
    data.forEach(function (t) {
      res.push(t[key]);
    });
  }
  return res;
};

const getSymbolData = (data) => {
  const arr = [];
  for (var i = 0; i < data.length; i++) {
    arr.push({
      value: data[i].y,
      symbolPosition: "end",
    });
  }
  return arr;
};

const generateArray = (arr) => {
  const max = Math.max(...arr.map((item) => item.y));
  return Array.from({ length: arr.length }, (_, index) => max);
};

const init = async () => {
  const [res] = await ajax.laborBrokerage();
  personnelCount.value = res.personnelCount;
  resumeCount.value = res.resumeCount;
  certCount.value = res.certCount;
  const { chart } = res;
  option.yAxis[0].data = getArrByKey(chart, "x");
  option.yAxis[1].data = getArrByKey(chart, "x");
  option.series[0].data = getSymbolData(chart); //条形末端圆点
  option.series[1].data = getSymbolData(generateArray(chart)); //背景
  option.series[2].data = getSymbolData(chart); //条
  option.yAxis[0].axisLabel.formatter = (v, i) => "{title|" + chart[i].x + "}";
  option.yAxis[1].axisLabel.formatter = (v, i) =>
    chart[i].y + "{percentNumber|}";
};

onMounted(() => {
  init();
});
</script>

<style scoped lang='scss'>
.labor-brokerage {
  margin-top: 47px;
  .title {
    height: 80px;
    background-image: url("@/assets/bigscreen2/小标题背景.png");
    padding-top: 15px;
    box-sizing: border-box;
    .title-text {
      margin-left: 68px;
      font-size: 22px;
      font-family: JLinXin;
      font-weight: 600;
      line-height: 22px;
      background: #fffde8 linear-gradient(180deg, #e8feff 40%, #00a9ff 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }
  .data-display {
    height: 98px;
    margin-left: 17px;
    display: flex;
    justify-content: space-between;
    .data-item {
      margin-top: 18px;
      .item-title {
        display: flex;
        align-items: center;
        margin-bottom: 4px;
        .border {
          display: inline-block;
          width: 3px;
          height: 13px;
          background: #289583;
          border-radius: 2px;
          margin-right: 8px;
        }
      }
      .item-number {
        font-size: 26px;
        font-family: D-DIN-PRO-Heavy, D-DIN-PRO;
        font-weight: 800;
        color: #ffffff;
        line-height: 26px;
        letter-spacing: 1px;
        margin-left: 7px;
      }
      .key {
        font-size: 14px;
        font-family: SourceHanSansCN-Medium, SourceHanSansCN;
        font-weight: 500;
        color: #ffffff;
        line-height: 21px;
        opacity: 0.8;
        margin-left: 17px;
      }
    }
  }
  .chart-box {
    height: 229px;
    color: #fff;
    position: relative;
    .chart-title {
      position: absolute;
      width: 481px;
      height: 32px;
      z-index: 2;
      .text {
        padding-left: 13px;
        font-size: 16px;
        font-family: SourceHanSansCN-Bold, SourceHanSansCN;
        font-weight: bold;
        color: #fffde8;
      }
      .legend {
        float: right;
        font-size: 12px;
        margin-right: 10px;
        display: flex;
        align-items: center;
        letter-spacing: 2px; /* 设置字间距为2像素 */
        .legend-color {
          width: 16px;
          height: 6px;
          background: #1bd3df;
          display: inline-block;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>